package com.dongdongshop.mq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.dongdongshop.send_sms.Sample;
import com.dongdongshop.service.FreemarkerService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(topic = "phone",consumerGroup = "usermsm-group")
public class PhoneSmsRokcetMQconsumer implements RocketMQListener<Long[]>, RocketMQPushConsumerLifecycleListener {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    MessageExt messageExt = list.get(0);
                    byte[] bodys = messageExt.getBody();
                    String phone = new String(bodys);

                    com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
                    //随机生成六位验证码
                    String random = RandomStringUtils.randomNumeric(6);
                    SendSmsRequest sendSmsRequest = new SendSmsRequest()
                            .setPhoneNumbers(phone)
                            .setSignName("东科创想")
                            .setTemplateCode("SMS_162522027")
                            .setTemplateParam("{\"sms_code\":\"" + random + "\"}");
                    //将发送的验证码放入redis当中
                    ValueOperations valueOperations = redisTemplate.opsForValue();
                    valueOperations.set(phone,random,1000 * 5, TimeUnit.MINUTES);
                    // 复制代码运行请自行打印 API 的返回值
                    SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
                    System.out.println(sendSmsResponse.getBody().getCode());

                } catch (Exception e) {
                   return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }

                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }

    @Override
    public void onMessage(Long[] longs) {

    }
}
